<template>
  <div>
    <div>
      <el-input v-model="jl.name" placeholder="添加职称等级" style="width: 300px"></el-input>
      <el-select v-model="jl.titleLevel" placeholder="职称等级" style="margin: 0 15px">
        <el-option
          v-for="item in titleLevels"
          :key="item"
          :label="item"
          :value="item">
        </el-option>
      </el-select>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addJoblevel">添加</el-button>
    </div>
    <div>
      <el-table
        :data="jls"
        stripe
        style="width: 80%"
        @selection-change="handleSelectionChange">
        <el-table-column
        type="selection"
        align="center">
        </el-table-column>
        <el-table-column
          type="index"
          label="#"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="name"
          label="职称名称"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="titleLevel"
          label="职称等级"
          align="center">
        </el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          align="center">
        </el-table-column>
        <el-table-column
          prop="enabled"
          label="是否启用"
          align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled == true">启用</el-tag>
            <el-tag v-else type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="编制职称"
        :visible.sync="dialogVisible"
        width="30%">
        <div style="margin-top: 15px">
          <el-tag>职称名称</el-tag>
          <el-input v-model="editjoblevel.name" placeholder="请输入职称名称" style="margin-top: 10px"></el-input>
        </div>
        <div style="margin-top: 15px">
          <el-tag>职称等级</el-tag>
          <div style="margin-top: 10px">
            <el-select v-model="editjoblevel.titleLevel" placeholder="请选择职称等级" style="width: 100%">
            <el-option 
              v-for="item in titleLevels"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
          </div>
          <div style="margin-top: 15px">
            <el-tag>是否启用</el-tag>
            <div style="margin-top: 10px">
              <el-switch 
              v-model="editjoblevel.enabled" 
              active-color="#13ce66" 
              inactive-color="#ff4949" 
              style="margin-top: 10px">
              </el-switch>
            </div>
          </div>
        </div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doUpdata">确 定</el-button>
          </span>
        </template>
      </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
     jl: {
       name: '',
       titleLevel: ''
     },
     titleLevels: [
       "正高级",
       "副高级",
       "中级",
       "初级",
       "员级"
     ],
     jls: [],
     dialogVisible: false,
     editjoblevel: {
       id: '',
       name: '',
       titleLevel: '',
       enabled: true
     }
    }
  },
  created () {
     this.initJls();
  },
  methods: {
    doUpdata () {
      this.putRequest('/system/basic/joblevel/', this.editjoblevel).then(resp => {
        if (resp) {
          this.dialogVisible = false
          this.initJls()
        }
      })
    },
    handleEdit (data) {
      Object.assign(this.editjoblevel, data)
      this.editjoblevel.createDate = ''
      this.dialogVisible = true
    },
    handleDelete (data) {
      this.deleteRequest('/system/basic/joblevel/' + data.id).then(resp => {
        if (resp) {
          this.initJls()
        }
      })
    },
    addJoblevel () {
      if ( this.jl.name && this.jl.titleLevel) {
        this.postRequest('/system/basic/joblevel/', this.jl).then(resp => {
          if (resp) {
            this.initJls()
          }
        })
      }
    },
    initJls () {
      this.getRequest('/system/basic/joblevel/').then(resp => {
        if (resp) {
          this.jls = resp
          this.jl ={}
        }
      })
    },
    handleSelectionChange () {

    }
  }
}
</script>

<style lang='less' scoped>

</style>
